package com.aloda.controller.common;

import com.aloda.constant.ExceptionConstant;
import com.aloda.exception.BaseException;
import com.aloda.result.Result;
import com.aloda.service.PictureService;
import com.aloda.utils.FileCheckUtils;
import com.aloda.vo.picture.PicUploadVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 云对象存储图片上传controller
 * @date 2025/04/05
 */
@Slf4j
@RestController
@ConditionalOnProperty(prefix = "dada.CloudUploadController", name = "enable", matchIfMissing = false)
public class CloudUploadController {

    @Resource
    private PictureService pictureService;

    /**
     * 管理端图片上传
     * @param file
     * @return
     */
    @PostMapping("/admin/common/upload")
    public Result<PicUploadVO> upload(MultipartFile file){
        if (!FileCheckUtils.checkFile(file)) {
            throw new BaseException(ExceptionConstant.UPLOAD_ERROR);
        }
        PicUploadVO picUploadVO = pictureService.adminCloudUpload(file);
        log.info("管理端图片上传：{}",picUploadVO.getUrl());
        return Result.success(picUploadVO);
    }

    /**
     * 用户端头像图片上传
     * @param file
     * @return
     */
    @PostMapping("/user/common/upload")
    public Result<PicUploadVO> userAvatarUpload(MultipartFile file) {
        if (!FileCheckUtils.checkFile(file)) {
            throw new BaseException(ExceptionConstant.UPLOAD_ERROR);
        }
        PicUploadVO picUploadVO = pictureService.userCloudUpload(file);
        log.info("用户端头像图片上传：{}",picUploadVO.getUrl());
        return Result.success(picUploadVO);
    }

    /**
     * 用户端应用封面上传
     * @param file
     * @return
     */
    @PostMapping("/user/common/upload/cover")
    public Result<PicUploadVO> appCoverUpload(MultipartFile file) {
        if (!FileCheckUtils.checkAppCoverImgFile(file)) {
            throw new BaseException(ExceptionConstant.UPLOAD_ERROR);
        }
        PicUploadVO picUploadVO = pictureService.userCloudUpload(file);
        log.info("用户端应用封面上传：{}",picUploadVO.getUrl());
        return Result.success(picUploadVO);
    }
}
